package until;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class ConnectSql {
    public static void main(String []args){
        try {
            Class.forName("com.mysql.jdbc.Driver");//序列化驱动
            String url = "jdbc:mysql://127.0.0.1:3306/work";//SQL链接，中括号需要替换自己数据库信息
            String username = "root";//数据库帐号
            String password = "admin";//数据库密码
            Connection connection = DriverManager.getConnection(url, username, password);//配置链接驱动
            System.out.println("===> " + connection);
            String sql = "select * from temp"; //SQL例子
            PreparedStatement preparedStatement = connection.prepareStatement(sql);//执行SQL语句
            ResultSet resultSet = preparedStatement.executeQuery();//SQL结果集
            while (resultSet.next()) {//如果有结果就开始循环
                System.out.println("--> " + resultSet.getString("name"));//打印查询结果
            }
            //关闭数据库链接
            resultSet.close();
            preparedStatement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{

        }
    }
}
